﻿using BUSINESS;
using DataAccessObject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI.GiaoVien
{
    /// <summary>
    /// Interaction logic for GUIBanHoc.xaml
    /// </summary>
    public partial class GUIBanHoc : Window
    {
        int _noOfErrors = 0;
        List<BANLOPHOC> listBanHoc;
        public GUIBanHoc()
        {
            InitializeComponent();
            ///////////////////////
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeControl();
        }

        private void InitializeControl()
        {
            listBanHoc = BanHocBUS.getAllBanLopHoc();
            txtListBanHoc.ItemsSource = listBanHoc; 
            txtListBanHoc.Items.Refresh();
            if (txtListBanHoc.Items.Count > 0) txtListBanHoc.SelectedIndex = 0;
            ////////////////////////////////////////////////////////
        }

        private void txtListBanHoc_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var banhoc = txtListBanHoc.SelectedItem as BANLOPHOC;
            if (banhoc == null) return;
            txtListLopThuocBan.ItemsSource = LopHocBUS.getLopHocByBanHoc(banhoc);
            txtListLopThuocBan.Items.Refresh();
            ///////////////////////////////////
            txtInputTenBanHoc.Text = banhoc.TenBan  ;
            setUpdateState();
            ///////////////////////////////////
            tbMonHocBan.ItemsSource = MonHocBUS.getMonHocByBanHoc(banhoc);
            tbMonHocBan.Items.Refresh();
        }

        private void Add_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _noOfErrors == 0;
            e.Handled = true;
        }

        private void Add_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            btnLuuLai_Click();
        }

        private void Validation_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _noOfErrors++;
            else
                _noOfErrors--;
        }

        private void setUpdateState()
        {
            btnCapNhat.IsEnabled = true;
            btnLuuLai.IsEnabled = false;
        }

        private void setSaveState()
        {
            btnCapNhat.IsEnabled = false;
            btnLuuLai.IsEnabled = true;
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            txtInputTenBanHoc.Text = "";
            setSaveState();
        }

        private void btnLuuLai_Click()
        {
            var tenbanhoc = txtInputTenBanHoc.Text;
            var banhoc = new BANLOPHOC() { TenBan = tenbanhoc };
            if (banhoc == null) return;
            banhoc = BanHocBUS.insertBanHoc(banhoc);
            listBanHoc.Add(banhoc);
            txtListBanHoc.Items.Refresh();            
        }

        private void tbMonHocBan_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var mon_ban = tbMonHocBan.SelectedItem as MONHOC_BAN;
            if (mon_ban == null) return;
            txtInputHeSo.SelectedIndex = mon_ban.HeSoDiem > 0 ? mon_ban.HeSoDiem - 1 : 0;
            txtInputTenMonHoc.DataContext = mon_ban.MONHOC;
        }

        private void btnXacNhan_Click(object sender, RoutedEventArgs e)
        {
            var mon_ban = tbMonHocBan.SelectedItem as MONHOC_BAN;
            if (mon_ban == null) return;
            mon_ban.HeSoDiem = Int32.Parse((txtInputHeSo.SelectedItem as ComboBoxItem).Content.ToString());
            BanHocBUS.insertMonHoc_Ban(mon_ban);
        }

        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            var tenmonban = txtInputTenBanHoc.Text;
            if (tenmonban == "" || tenmonban == null) return;
            var banhoc = txtListBanHoc.SelectedItem as BANLOPHOC;
            if (banhoc == null) return;
            banhoc.TenBan = tenmonban;
            BanHocBUS.updateBanHoc(banhoc);
        }
    }
}
